package com.atm.servlet;

import com.atm.pojo.User;
import com.atm.service.UserService;
import com.atm.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用户登录
 */
@WebServlet("/UserLogin")
public class UserLoginServlet extends HttpServlet {
    private UserService userService =new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String icno = req.getParameter("icno");
        String pwd = req.getParameter("pwd");
        //验证
        User user=userService.findUserByNameAndPwd(icno, pwd);
        if (user==null){
            req.setAttribute("error","用户登录失败");
            req.getRequestDispatcher("/changepage/login.jsp").forward(req,resp);
        }else {
            req.getSession().setAttribute("ua",user);
            resp.sendRedirect("/changepage/UserIndex.jsp");
        }
    }
}
